package com.sgb.zhaocai.view.demand.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.tabs.TabLayout;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.ActivityOfferQueryListBinding;
import com.sgb.zhaocai.entity.PublishListBean;
import com.sgb.zhaocai.view.demand.fragment.OfferListFragment;
import com.sgb.zhaocai.viewmodel.OfferQueryListModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class OfferQueryListActivity extends AppActivity {

    private ActivityOfferQueryListBinding mBinding;
    private OfferQueryListModel mModel;
    private ArrayList<Fragment> mFragments;
    private String SearchName ="";
    private List<PublishListBean> titledata;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, OfferQueryListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_offer_query_list);
        mModel = new ViewModelProvider(this).get( OfferQueryListModel.class);
        mBinding.includeTitle.ivBack.setOnClickListener(v -> finish());
        mBinding.includeTitle.titleView.setText("报价查询");
        //搜索
        mBinding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentListData();
            }
        });
        mBinding.editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length()>1){
                    mBinding.searchDelete.setVisibility(View.VISIBLE);
                }else {
                    mBinding.searchDelete.setVisibility(View.GONE);
                }
            }
        });
        mBinding.searchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBinding.editSearch.setText("");
                mBinding.searchDelete.setVisibility(View.GONE);
                FragmentListData();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mModel.getTabData(this);
    }
    public void setTabList(List<PublishListBean> data) {
        if (null == data||data.size()==0) {
            return;
        }
        this.titledata = data;
        String[] mTitles = new String[data.size()];
        mFragments = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            mTitles[i]=(data.get(i).getName());
            mFragments.add(OfferListFragment.newInstance(TextUtils.isEmpty(data.get(i).getNo())?"0":data.get(i).getNo()));
        }
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mBinding.viewPager.setOffscreenPageLimit(mTitles.length);
        mBinding.viewPager.setAdapter(mPagerAdapter);
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        mBinding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < mBinding.tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mBinding.tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
        updateTabTextView(mBinding.tabLayout.getTabAt(mBinding.tabLayout.getSelectedTabPosition()), true);
        mBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabTextView(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabTextView(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    private View getTabView(int currentPosition) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.tab_item_textview, null);
        TextView textView = (TextView) view.findViewById(R.id.tv_tab_item_textview);
        textView.setTextColor(getResources().getColor(R.color.color_tab_two));
        textView.setTextSize(14);
        textView.setText(titledata.get(currentPosition).getName());
        return view;
    }
    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        if (tab == null || tab.getCustomView() == null) return;
        if (isSelect) {
            //选中加粗
            TextView tabSelect = (TextView) tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
            tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tabSelect.setTextColor(getResources().getColor(R.color.color_main));
            tabSelect.setTextSize(14);
            tabSelect.setText(tab.getText());
        } else {
            TextView tabUnSelect = (TextView) tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
            tabUnSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tabUnSelect.setTextColor(getResources().getColor(R.color.color_tab_two));
            tabUnSelect.setTextSize(14);
            tabUnSelect.setText(tab.getText());
        }
    }


    private void FragmentListData() {
        Fragment fragment = mFragments.get(mBinding.viewPager.getCurrentItem());
        if (fragment!=null&&fragment instanceof OfferListFragment) {
            ((OfferListFragment) fragment).initializePop(TextUtils.isEmpty(mBinding.editSearch.getText().toString().trim())?"":mBinding.editSearch.getText().toString().trim());
        }
    }
}
